﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;
using QLTV_V2.DataAccess;


namespace QLTV_V2
{
    public partial class FrmTaoPhieuKK : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmTaoPhieuKK()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        SachBUS sachBUS = new SachBUS();
        PhieuKiemKe pkk = new PhieuKiemKe();
        public string manv;
        private void FrmTaoPhieuKK_Load(object sender, EventArgs e)
        {
            radio_TaoMoi.Checked = true;
            //Load ngay giờ
            date_NgayKK.Value = System.DateTime.Now;
            //Load combobox phieu kk
            comb_phieuKK.DataSource = uti.getDataSource("PhieuKiemKe", "MaPhieuKK");
            comb_phieuKK.DisplayMember = "TenPhieuKK";
            comb_phieuKK.ValueMember = "MaPhieuKK";
        }

        private void btn_Dongy_Click(object sender, EventArgs e)
        {
            if (radio_TaoMoi.Checked == true)
            {
                if (MessageBox.Show("Bạn có muốn tạo mới đợt kiểm kê không?", "Hỏi",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    pkk = getTT_PhieuKK();
                    if (sachBUS.insertPhieuKK(pkk) == true)
                    {
                        FrmCTKiemke frm = new FrmCTKiemke();
                        frm.dk = 1;
                        frm.maphieukk = txt_MaKK.Text;
                        frm.ShowDialog();
                    }
                    this.Close();
                }
            }
            if (Radio_TaoSan.Checked == true)
            {
                FrmCTKiemke frm = new FrmCTKiemke();
                frm.dk = 2;
                frm.maphieukk = comb_phieuKK.SelectedValue.ToString();
                frm.ShowDialog();
            }
        }
        PhieuKiemKe getTT_PhieuKK()
        {
            pkk.MaPhieuKK = txt_MaKK.Text.ToUpper();
            pkk.TenPhieuKK = txt_LydoKK.Text;
            pkk.NgayKK = System.DateTime.Now;
            pkk.MaNV = manv.ToUpper();
            return pkk;
        }

        private void radio_TaoMoi_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                groupPanel1.Enabled = true;
                if (radio_TaoMoi.Checked == true)
                {
                    string lastID = uti.GetLastID("PhieuKiemKe", "MaPhieuKK", "MaPhieuKK", "PKK-0000");
                    string nextID = uti.GetNextID(lastID, "PKK-");
                    txt_MaKK.Text = nextID.ToUpper();
                    txt_LydoKK.Text = "";
                    date_NgayKK.Value = System.DateTime.Now;
                    comb_phieuKK.Enabled = false;
                }
            }
            catch
            { 
            
            }
        }

        private void Radio_TaoSan_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (Radio_TaoSan.Checked == true)
                {
                    groupPanel1.Enabled = false;
                    string makk = comb_phieuKK.SelectedValue.ToString();
                    pkk = sachBUS.getPhieuKK_object(makk);
                    txt_MaKK.Text = pkk.MaPhieuKK;
                    txt_LydoKK.Text = pkk.TenPhieuKK;
                    date_NgayKK.Value = pkk.NgayKK;
                    //Load trạng thái
                    comb_phieuKK.Enabled = true;
                    btn_XoaPhieu.Enabled = true;
                }
            }
            catch
            { 
            
            }
        }

        private void comb_phieuKK_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (Radio_TaoSan.Checked == true)
                {
                    groupPanel1.Enabled = false;
                    string makk = comb_phieuKK.SelectedValue.ToString();
                    pkk = sachBUS.getPhieuKK_object(makk);
                    txt_MaKK.Text = pkk.MaPhieuKK;
                    txt_LydoKK.Text = pkk.TenPhieuKK;
                    date_NgayKK.Value = pkk.NgayKK;
                }
            }
            catch
            { }
        }

        private void btn_XoaPhieu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn hủy bỏ đợt kiểm kê này không?", "Hỏi",
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (uti.deleteTable("PhieuKiemKe", "MaPhieuKK", comb_phieuKK.SelectedValue.ToString()) == true)
                {
                    MessageBox.Show("Đã xóa");
                    FrmTaoPhieuKK_Load(sender, e);
                }
                else
                    MessageBox.Show("Không thể xóa");
            }
        }
    }
}
